<?php

namespace app\admin\controller\user;

use app\common\controller\Backend;
use app\common\library\Ali;
use think\Db;
use think\Exception;

/**
 * 用户提现
 *
 * @icon fa fa-circle-o
 */
class Withdraw extends Backend
{

    /**
     * Withdraw模型对象
     * @var \app\common\model\user\Withdraw
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\user\Withdraw;
        $this->view->assign("typeList", $this->model->getTypeList());
        $this->view->assign('statusList', $this->model->getStatusList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->with(['user'])
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);

            foreach ($list as $row) {


            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    public function tongguo($id = null)
    {
//        $ids = $this->request->param('id');
        $row = $this->model->get($id);
        if (!$row) {
            $this->error(__('No Results were found'));
        }

        $user = Db::name('user')->where('id', $row['user_id'])->find();
        if (!$user) {
            $this->error("当前用户有误");
        }

        if ($row['with_method'] == 2) {
            $mobile = $user['wx_account'];
            $this->error("微信提现未放");
        } elseif ($row['with_method'] == 1) {
            $mobile = $user['ali_account'];
        } else {
            $this->error("提现方式有误");
        }
        $app = Ali::util();
        $sn = get_sn($user['id']);
        $bizParams = [
            "out_biz_no" => $sn,
            "remark" => "提现{$user['id']}",
            "biz_scene" => "DIRECT_TRANSFER",
            "trans_amount" => $row['price'],
            "product_code" => "TRANS_ACCOUNT_NO_PWD",
            "order_title" => date("Ymd") . "提现",
            "business_params" => [
                "payer_show_name_use_alias" => true
            ],
            "payee_info" => [
                "identity" => $mobile,
                "identity_type" => "ALIPAY_LOGON_ID",
                "name" => $user['real_name']
            ]
        ];
        $result = $app->generic()->execute("alipay.fund.trans.uni.transfer", [], $bizParams);
        if ($result->code == 10000 && $result->msg == 'Success') {
            Db::startTrans();
            try {
                if (isset($sn)) {
                    $row->with_sn = $sn;
                }
                $row->status = 1;
                $row->save();
                Db::commit();
            } catch (Exception $exception) {
                Db::rollback();
                $this->error($exception->getMessage());
            }
        } else {
            $this->error($result->subMsg);
        }

        $this->success();
    }

    public function jujue($id = null)
    {
        $id = empty($id) ? $this->request->request('id') : $id;
        $this->view->assign('id', $id);
        if ($this->request->isPost()) {
            $cause = $this->request->request('desc');
            $row = $this->model->get($id);
            Db::startTrans();
            try {
                $row->status = 2;
                $row->desc = $cause;
                $row->save();
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            $this->success();
        }
        return $this->view->fetch();
    }

    public function manual($id = null)
    {
        $row = $this->model->get($id);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        Db::startTrans();
        try {
            $row->status = 1;
            $row->with_method = 3;
            $row->save();
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success();

    }
}
